package com.egame.ewidget.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

public class Utils {
	public static String TAG = "Utils";

	public static boolean isIntentOkay(Intent intent) {
		if (intent == null) {
			Log.e(TAG, "Null intent!");
			return false;
		}
		String action = intent.getAction();
		if (action == null) {
			Log.e(TAG, "Null intent action!");
			return false;
		}
		return true;
	}

	public static void log(String message) {
		if (Constants.LOG)
			Log.d(Constants.LOG_TAG, message);
	}

	public static boolean hasPermission(Context context, String appPackageName,
			String permission) {
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(
					appPackageName, PackageManager.GET_PERMISSIONS);
			if (info.requestedPermissions != null) {
				for (String p : info.requestedPermissions) {
					if (p.equals(permission)) {
						return true;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean appInstalledOrNot(Context context, String packageName) {
		PackageManager pm = context.getPackageManager();
		try {
			pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
			return true;
		} catch (PackageManager.NameNotFoundException e) {
			return false;
		}
	}
}
